import Vue from 'vue'

//注册状态管理器插件
import Vuex from 'vuex'
//引入数据持久化储存插件
import persistedstate from 'vuex-persistedstate'
Vue.use(Vuex)

//实例化状态管理器
const store=new Vuex.Store({
    plugins:[
        persistedstate({
            storage: window.sessionStorage
        })
    ],
    modules:{
        common:{
            namespaced:true,
            state:{
                token:'',
                userInfo:'',
                projectInfo:'',
                shoppingInfo:'',
                orderInfo:'',
                address:[],
                //我的订单
                myorders:[]
            },
            getters:{
                getToken(state){
                    return state.token
                },
                getUserInfo(state){
                    return state.userInfo
                },
                getProjectInfo(state){
                    return state.projectInfo
                },
                getShoppingInfo(state){
                    return state.shoppingInfo
                },
                getOrderInfo(state){
                    return state.orderInfo
                },
                getAddress(state){
                    return state.address
                },
                getMyorders(state){
                    return state.myorders
                }
            },
            mutations:{
                setToken(state,token){
                    state.token=token
                },
                setUserInfo(state,user){
                    state.userInfo=user
                },
                setProjectInfo(state,project){
                    state.projectInfo=project
                },
                setShoppingInfo(state,shopping){
                    state.shoppingInfo=shopping
                },
                setOrderInfo(state,orderInfo){
                    state.orderInfo=orderInfo
                },
                setAddress(state,address){
                    state.address=address
                },
                setMyorders(state,myorders){
                    state.myorders=myorders
                }
            },
            actions:{
                asyncSetToken(store,token){
                    setTimeout(function(){
                        store.commit('getToken',token)
                    })
                }
            }
        }
    }
})

export default store